package riaxprez;

import javafx.animation.FadeTransition;
import javafx.animation.ScaleTransition;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Duration;

public class ViewPrez {

    public ViewPrez(AppModel model) {
        this.model = model;
        stage = new Stage();
        init(stage);
    }

    private void init(Stage stage) {
        /* Panel principal */
        root = new BorderPane();

        /* Panel du haut contenant le titre */
        pnTop = new HBox();
        pnTop.setPrefWidth(200);
        pnTop.setPrefHeight(100);
        pnTop.setAlignment(Pos.BOTTOM_CENTER);
        pnTop.setPadding(new Insets(20, 20, 20, 30));

        lbTitle = new Label("EIA - PREZ - Creation");
        lbTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 30));
        pnTop.getChildren().add(lbTitle);

        /* Panel du centre contenant les boutons ouvrir/creer */
        scrollPane = new ScrollPane();
        scrollPane.setPrefSize(1000, 600);
        pnCenter = new VBox();

        pagination = new Pagination(model.getNbPage());
        pagination.setPrefSize(1100, 500);
        pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
        pagination.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer pageIndex) {
                if (pageIndex >= model.getNbPage()) {
                    return null;
                } else {
                    return createPage(pageIndex);
                }
            }
        });
        scrollPane.setContent(pagination);
        scrollPane.setFitToWidth(true);
// pnCenter.getChildren().add(pagination);
        pnCenter.getChildren().add(scrollPane);
        HBox bot = new HBox(4);
        btnSwitchToCreation = new Button("Mode création");
        btnFullScreen = new Button("Full Screen");
        stageFullScreen = new Stage();
        page = (BorderPane) pagination.getPageFactory().call(new Integer(0));
        sceneFullScreen = new Scene(page, 1100, 500);
        stageFullScreen.setScene(sceneFullScreen);

        bot.getChildren().add(btnSwitchToCreation);
        bot.getChildren().add(btnFullScreen);
        /* Assignation des panels au panel principal */
        root.setTop(pnTop);
        root.setCenter(pnCenter);
        root.setBottom(bot);

        /* Assignation du panel principal a la scene */
        scene = new Scene(root, 1200, 800);
        stage.setTitle("Accueil");
        stage.setScene(scene);
        stage.show();
    }

    public BorderPane createPage(int pageIndex) {
        final BorderPane box = new BorderPane();

        for (int i = pageIndex; i < pageIndex + 1; i++) {
        //Ajout du titre
            titre = new Label(model.slides.get(i).getTitre());
            titre.setFont(Font.font("Tahoma", FontWeight.NORMAL, 80));
            box.setAlignment(titre, Pos.CENTER);
            box.setTop(titre);

            VBox vb = new VBox();

            WebView webView = new WebView();
            WebEngine webEngine = webView.getEngine();
            webView.setDisable(true);
            contenu = new Text();
            contenu.setText(model.slides.get(i).getContenu());
            webEngine.loadContent(contenu.getText());
            vb.getChildren().add(webView);

            boolean isGraph = model.slides.get(i).isGraphique();

            if (isGraph) {
                graphique = new PieChart(model.getSlides().get(pageCourante).getListeData());
                graphique.setTitle(model.getSlides().get(pageCourante).getTitreGraph());
                vb.getChildren().add(graphique);
            }
            box.setCenter(vb);


        }
        return box;
    }

    public void changePage() {
// On test si on est a la fin
        if (++pageCourante == model.getNbPage()) {
            pageCourante = model.getNbPage() - 1;
        }
// Transition entre 2 slides
        if (model.getTransition().equals("Fondu")) {
            page = (BorderPane) pagination.getPageFactory().call(
                    new Integer(pageCourante));
            sceneFullScreen.setRoot(page);
            fadeTransition();
        } else if (model.getTransition().equals("Zoom")) {
            ScaleTransition1();
            scaleTransition1.setOnFinished(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
// TODO Auto-generated method stub
                    page = (BorderPane) pagination.getPageFactory().call(
                            new Integer(pageCourante));
                    page.setVisible(false);
                    sceneFullScreen.setRoot(page);
                    ScaleTransition2();
//Enleve bug daffichage, affiche seulement apres le depart de lanimation!
                    scaleTransition2.currentTimeProperty().addListener(
                            new ChangeListener<Duration>() {
                                @Override
                                public void changed(ObservableValue<? extends Duration> ov, Duration t, Duration t1) {
                                    page.setVisible(true);
                                }
                            });
                }
            });
        }
    }
// depart en fon de lanimation
    public void ScaleTransition1() {
        scaleTransition1 = new ScaleTransition(Duration.millis(1000), sceneFullScreen.getRoot());
        scaleTransition1.setToX(0);
        scaleTransition1.setToY(0);
        scaleTransition1.setCycleCount(1);
        scaleTransition1.play();
    }
// retour de lanimation
    public void ScaleTransition2() {
        scaleTransition2 = new ScaleTransition(Duration.millis(1000), sceneFullScreen.getRoot());
        scaleTransition2.setFromX(0);
        scaleTransition2.setFromY(0);
        scaleTransition2.setToX(1);
        scaleTransition2.setToY(1);
        scaleTransition2.setCycleCount(1);
        scaleTransition2.play();
    }

    public void fadeTransition() {
        fadeTransition = new FadeTransition(Duration.millis(3000),
                sceneFullScreen.getRoot());
        fadeTransition.setFromValue(0.0);
        fadeTransition.setToValue(1.0);
        fadeTransition.setCycleCount(1);
        fadeTransition.setAutoReverse(false);
        fadeTransition.play();
    }
    AppModel model;

    /* IHM */
    Stage stage;
    Scene scene;
    Stage stageFullScreen;
    Scene sceneFullScreen;
    Pagination pagination;

    /* Layout / Panel */
    BorderPane root;
    HBox pnTop;
    VBox pnCenter;
    ScrollPane scrollPane;
    BorderPane page;
    /* Labels */
    Label lbTitle;
    Label titre;
    Text contenu;

    /* Boutons */
    Button btGraphAdd;
    Button btnSave;
    Button btnSwitchToCreation;
    Button btnFullScreen;

    /* Graphique */
    PieChart graphique;
    Button btAjoutPart;
    Button btSupprPart;
    Button btSupprAll;
    int pageCourante = 0;
    boolean isGraph;
    private FadeTransition fadeTransition;
    private ScaleTransition scaleTransition1;
    private ScaleTransition scaleTransition2;
}